from lxml import etree

if __name__ == '__main__':
    root = etree.Element("root")
    # 在元素的内部添加文本内容
    root.text = "content"
    # 在元素的外部添加文本内容
    root.tail = "abcd"
    print(etree.tostring(root))

    parent = etree.Element("parent", color="red")
    print(etree.tostring(parent))
    # 美化输出
    print(etree.tostring(parent, pretty_print=True).decode("utf-8"))

    test = etree.Element("div", {"class": "item"})
    print(etree.tostring(test, pretty_print=True).decode("utf-8"))